package com.whz.gateway.configure.access.rules.black;

import com.whz.gateway.common.GatewayMessageAccessor;
import com.whz.gateway.common.util.GatewayIpUtils;
import com.whz.gateway.configure.sensitive.IpSensitiveService;
import com.whz.gateway.limit.IBlackRule;
import com.whz.generic.spring.SpringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

/**
 * 用户remoteIp命中黑名单的禁止访问
 *
 * @author whz
 */
@Component
public class IpForbiddenRule implements IBlackRule {

    @Override
    public String getName() {
        return "IpForbiddenRule";
    }

    @Override
    public String getMessage() {
        return GatewayMessageAccessor.getMessage("gateway.config.rules.black.IpForbiddenRule.IP_FORBIDDEN");
    }

    @Override
    public boolean test(ServerWebExchange exchange) {
        return SpringUtils.getBean(IpSensitiveService.class).map(ipSensitiveService -> ipSensitiveService.matchBlackIp(
                GatewayIpUtils.getRemoteIp(exchange.getRequest()))).orElse(false);
    }

}